package controller.users;

import java.util.GregorianCalendar;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.Transaction;
import org.hibernate.Session;

import utils.HibernateUtils;

import users.User;

public class RegisterAction extends Action {
	
	private static Session s = null;
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res) throws Exception {
		String resultat = null;
		String name = ((RegisterForm) form).getName();
		String firstname = ((RegisterForm) form).getFirstname();
		String mail = ((RegisterForm) form).getMail();
		String pass = ((RegisterForm) form).getPass();
		
		s = HibernateUtils.getSession();
		Transaction t = s.beginTransaction();
		
		User u = new User();
		u.setName(name);
		u.setFirstname(firstname);
		u.setPass(pass);
		u.setMail(mail);
		u.setRole(false);
		
		s.save(u);
		t.commit();
		s.close();
		
		resultat = "succes";
		/*if (name.equals("romain") && pass.equals("romain")) {
		resultat = "succes";
		} else {
		resultat = "echec";
		}*/
		
		return mapping.findForward(resultat);
	}

}
